//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;

//全局配置
$.ajaxSetup({
	dataType: "json",
	contentType: "application/json",
	cache: false
});

function hasPermission(permission) {
    if (window.parent.permissions.indexOf(permission) > -1) {
        return true;
    } else {
        return false;
    }
}

//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    return grid.getGridParam("selarrrow");
}

function map2List (map) {
    var list = []
    for (var key in map) {
        var obj = {}
        obj.value = parseInt(key)
        obj.text = map[key]
        list.push(obj)
    }
    return list
}

function list2Map(list) {
    var obj = {}
    for (var i = 0, len = list.length; i < len; i++) {
        obj[list[i].value] = list[i].text
    }
    return obj
}

function columnList2Map(list) {
    var obj = {}
    for (var i = 0, len = list.length; i < len; i++) {
        obj[list[i].field] = list[i].isShow
    }
    return obj
}

function aretaList2Map(list) {
    var obj = {}
    for (var i = 0, len = list.length; i < len; i++) {
        obj[list[i].code] = list[i].name
    }
    return obj
}

/**
 * 转成搜索框中的list 添加全部
 * @param list
 * @returns {Array}
 */
 function toSelectList (list, lang) {
    var list2 = []
    if (lang === 'zh') {
        list2.push({text: '全部', value: -1})
    } else {
        list2.push({text: 'All', value: -1})
    }

    for (var i = 0, len = list.length; i < len; i++) {
        if (list[i].value != null) {
            list2.push(list[i])
        }
    }
    return list2
}

/**
 * tree 增加 全部
 * @param list
 * @returns {Array}
 */
function toTreeAddRoot(list, lang) {
    var list2 = []
    if (lang === 'zh') {
        list2.push({text: '全部', value: '0', children: list})
    } else {
        list2.push({text: 'All', value: '0', children: list})
    }
    return list2
}

/**
 * DateFormat(date,"yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 * DateFormat(date,"yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 * @param date
 * @param fmt
 * @returns {*}
 * @constructor
 */
function DateFormat (date,fmt)
{ //author: meizz
    var o = {
        "M+" : date.getMonth()+1,                 //月份
        "d+" : date.getDate(),                    //日
        "h+" : date.getHours(),                   //小时
        "m+" : date.getMinutes(),                 //分
        "s+" : date.getSeconds(),                 //秒
        "q+" : Math.floor((date.getMonth()+3)/3), //季度
        "S"  : date.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}